package annotool;

import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.border.*;
import javax.swing.*;
import java.awt.event.*;

public class AnnControlPanel extends JPanel implements ActionListener {


	String[] channels = {  "red (channel 1)", "green (channel 2)", "blue (channel 3)", "all (channel 4)" };
	String[] channelInputs = {  "r", "g", "b", "all" };//actual input to algorithm
	String[] folds = {  "LOO (Leave One Out)", "5 fold CV", "10 fold CV" };
	String[] foldInputs = { "LOO" , "5", "10" };//actual input to algorithm

	String[] extractors = {  "wavelet/HAAR", "SIFT" , "None"};
	String[] selectors = {  "mRMR-MIQ", "mRMR-MID","SIFT", "None" }; //081007: add MID
	String[] classifiers = { "Support Vector Machine", "LDA"};
	String[] outputs = { "Classification Accuracy", "Annotation Table"};

	JLabel  chanL = new JLabel("Channel");
	JLabel  feL = new JLabel("Feature Extractor");
	JLabel  fsL = new JLabel ("Feature Selector");
	JLabel  classL = new JLabel ("Classifier");
	//JLabel  outputL = new JLabel ("Output");

	JComboBox chanBox = new JComboBox(channels);
	JComboBox fexBox = new JComboBox(extractors);
	JComboBox fslBox = new JComboBox(selectors);
	JComboBox classifierBox = new JComboBox(classifiers);
	//JComboBox outputBox = new JComboBox(outputs);
	JButton goButton = new JButton("Start Annotation");
	JProgressBar progressBar = new JProgressBar(0, 100);

	AnnOutputPanel outputPanel = new AnnOutputPanel();
	AnnTablePanel  tablePanel;  

	JRadioButton redButton = new JRadioButton(channels[0]); 
	JRadioButton greenButton  = new JRadioButton(channels[1]); 
	JRadioButton blueButton  = new JRadioButton(channels[2]);
	JRadioButton allButton  = new JRadioButton(channels[3]);
	JRadioButton looButton = new JRadioButton(folds[0]); 
	JRadioButton fold2Button = new JRadioButton(folds[1]); 
	JRadioButton fold3Button = new JRadioButton(folds[2]);
	
	public AnnControlPanel(JFrame frame)
	{
		progressBar.setValue(0);
		progressBar.setStringPainted(true);
		tablePanel = new AnnTablePanel(frame);
		//goButton.setEnabled(false);
		thingsEnabled(false);

		//left: parameter  //right: imagePanel
		JPanel  leftPanel =  AnnotatorGUI.createVerticalPanel(true);
		Border loweredBorder = new CompoundBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), 
				new EmptyBorder(5,5,5,5));
		leftPanel.setBorder(loweredBorder);
		tablePanel.setBorder(loweredBorder);
		leftPanel.setBackground(java.awt.Color.white);
		tablePanel.setBackground(java.awt.Color.white);

		//left down panel
		JPanel  ldPanel =  new JPanel();
		ldPanel.setLayout(new GridLayout(3, 3, 5, 5));
		ldPanel.add(feL);
		ldPanel.add(fexBox);
		ldPanel.add(fsL);
		ldPanel.add(fslBox);
		ldPanel.add(classL);
		ldPanel.add(classifierBox);
		//ldPanel.add(outputL);
		//ldPanel.add(outputBox);
		ldPanel.setBorder(new CompoundBorder(new TitledBorder(null, "components", 
				TitledBorder.LEFT, TitledBorder.TOP), new EmptyBorder(5,5,5,5))); 
		ldPanel.setBackground(java.awt.Color.white);

		//left control components
		//add radio button for channel and fold selection
		allButton.setSelected(true);
		redButton.setBackground(java.awt.Color.white);
		greenButton.setBackground(java.awt.Color.white);
		blueButton.setBackground(java.awt.Color.white);
		allButton.setBackground(java.awt.Color.white);
		ButtonGroup group = new ButtonGroup();
		group.add(redButton);
	    group.add(greenButton);
	    group.add(blueButton);
	    group.add(allButton);
        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
	    radioPanel.add(redButton);
	    radioPanel.add(greenButton);
	    radioPanel.add(blueButton);
	    radioPanel.add(allButton);
		radioPanel.setBorder(new CompoundBorder(new TitledBorder(null,"channels", 
		TitledBorder.LEFT, TitledBorder.TOP), new EmptyBorder(5,5,5,5))); 
	        
		looButton.setSelected(true);
		looButton.setBackground(java.awt.Color.white);
		fold2Button.setBackground(java.awt.Color.white);
		fold3Button.setBackground(java.awt.Color.white);
		ButtonGroup foldgroup = new ButtonGroup();
		foldgroup.add(looButton);
	    foldgroup.add(fold2Button);
	    foldgroup.add(fold3Button);
        JPanel foldPanel = new JPanel(new GridLayout(0, 1));
	    foldPanel.add(looButton);
	    foldPanel.add(fold2Button);
	    foldPanel.add(fold3Button);
		foldPanel.setBorder(new CompoundBorder(new TitledBorder(null,"fold in cross validation", 
		TitledBorder.LEFT, TitledBorder.TOP), new EmptyBorder(5,5,5,5))); 
		radioPanel.setBackground(java.awt.Color.white);
		foldPanel.setBackground(java.awt.Color.white);
		//radioPanel.setForeground(java.awt.Color.white);


		
		JPanel selectionPanel = new JPanel();
		selectionPanel.setLayout(new GridLayout(1,2));
		selectionPanel.add(foldPanel);
		selectionPanel.add(radioPanel);
		
		
		JPanel  lbPanel =  new JPanel();
		lbPanel.add(goButton);
		lbPanel.setBorder(new CompoundBorder(new TitledBorder(null,"control", 
				TitledBorder.LEFT, TitledBorder.TOP), new EmptyBorder(5,5,5,5))); 
		lbPanel.setBackground(java.awt.Color.white);

		//left lastBottom panel
		outputPanel.setBackground(java.awt.Color.white);

		//build left panel
		//leftPanel.add(luPanel);
		leftPanel.add(ldPanel);
		leftPanel.add(selectionPanel);
		leftPanel.add(lbPanel);
		leftPanel.add(outputPanel);
		leftPanel.add(progressBar);

		setLayout(new BorderLayout());
		add(leftPanel, BorderLayout.WEST);
		add(tablePanel, BorderLayout.CENTER);

		//action listeners ...
		addListeners();
	}	

	public JPanel createHorizontalPanel(boolean threeD) 
	{
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
		p.setAlignmentY(TOP_ALIGNMENT);
		p.setAlignmentX(LEFT_ALIGNMENT);
		if(threeD) {
			Border loweredBorder = new CompoundBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), 
					new EmptyBorder(5,5,5,5));
			p.setBorder(loweredBorder);
		}
		return p;
	}


	private void addListeners()
	{
		goButton.addActionListener(this); //get all values from all tabs!
		fexBox.addActionListener(this);  //if "None", gray extractor tab, otherwise, may add?
		fslBox.addActionListener(this);  //if "None", gray selector tab?
		
	    redButton.addActionListener(this);
	    greenButton.addActionListener(this);
	    blueButton.addActionListener(this);
	    allButton.addActionListener(this);
	    looButton.addActionListener(this);
	    fold2Button.addActionListener(this);
	    fold3Button.addActionListener(this);
	}

	//call back event handlers
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == redButton)
			Annotator.channel = channelInputs[0];
		else if (e.getSource() == greenButton)
			Annotator.channel = channelInputs[1];
		else if (e.getSource() == blueButton)
			Annotator.channel = channelInputs[2];
		else if (e.getSource() == allButton)
			Annotator.channel = channelInputs[3];
		
		if (e.getSource() == looButton)
			Annotator.fold = foldInputs[0];
		else if (e.getSource() == fold2Button)
			Annotator.fold = foldInputs[1];
		else if (e.getSource() == fold3Button)
			Annotator.fold = foldInputs[2];

		else if (e.getSource() == goButton)
			startWorking();

		//To Be Added: automatic collapsing or disabling unnecessary tabs
		//else  if (e.getSource() == fexBox) ...		//   else  if (e.getSource() == fslBox)
	}


	private void startWorking()
	{
		//get parameters
		System.out.println("before" + Annotator.output);
		getParametersFromGUI();
		System.out.println("after" + Annotator.output);

		goButton.setEnabled(false);
		//outputPanel.clearOutput();
		this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		//Annotator object contains mostly static (class) variables. 
		//I should create a Singleton here!!
		System.out.println("before" + Annotator.output);
		(new Annotator(progressBar, goButton, this, outputPanel)).startAnnotation();	
		System.out.println("after" + Annotator.output);

	}

	private void getParametersFromGUI()
	{
		Annotator.featureSelector = (String) fslBox.getSelectedItem();
 	    Annotator.featureExtractor = (String) fexBox.getSelectedItem();
 	    Annotator.classifierChoice = (String) classifierBox.getSelectedItem();

 	    //System.out.println(Annotator.featureExtractor);
		if (Annotator.featureExtractor.equals(extractors[0]))
			Annotator.featureExtractor = "HAAR"; 
		if (Annotator.classifierChoice.equals(classifiers[0]))
			Annotator.classifierChoice = "SVM"; 

		

		//From Other Tabs
		Annotator.featureNum = AnnotatorGUI.numFeatureField.getText().trim();
		//public static String selectorType = System.getProperty("mRMRType", DEFAULT_MRMRTYPE);
		Annotator.svmpara = AnnotatorGUI.svmParaField.getText().trim();
		Annotator.waveletLevel = AnnotatorGUI.levelField.getText().trim();
		Annotator.SIFT_InitialSigma = AnnotatorGUI.isigmaField.getText().trim();
		Annotator.SIFT_FdSize = AnnotatorGUI.fdsizeField.getText().trim();
		Annotator.SIFT_FdBins = AnnotatorGUI.fdbinsField.getText().trim();
		Annotator.SIFT_MinSize = AnnotatorGUI.misizeField.getText().trim();
		Annotator.SIFT_MaxSize = AnnotatorGUI.masizeField.getText().trim();
		Annotator.SIFT_Upscale = AnnotatorGUI.upscaleField.getText().trim();
		Annotator.SIFT_Scale = AnnotatorGUI.scaleField.getText().trim();
		
		//if(chanBox.isEnabled())		
		//	Annotator.channel = channelInputs[chanBox.getSelectedIndex()];

		//other stuff
		//public static String debugFlag =  System.getProperty("debug", DEFAULT_DEBUG);
		//public static String discreteFlag = System.getProperty("discreteflag",DEFAULT_DISCRETE);
		//public static String shuffleFlag = System.getProperty("shuffleflag",DEFAULT_SHUFFLE);
	}

	//get the table panel to be used by others for displaying images
	public AnnTablePanel getTablePanel()
	{
		return tablePanel;
	}

	public AnnOutputPanel getOutputPanel()
	{
		return outputPanel;
	}

	public void channelEnabled(boolean flag)
	{
		//chanL.setEnabled(flag);
		//chanBox.setEnabled(flag);
		redButton.setEnabled(flag);
		greenButton.setEnabled(flag);
		blueButton.setEnabled(flag);
		allButton.setEnabled(flag);
	}
	
	public void goEnabled(boolean flag)
	{
		goButton.setEnabled(flag);
	}
	
	public void foldEnabled(boolean flag)
	{
		looButton.setEnabled(flag);
		fold2Button.setEnabled(flag);
		fold3Button.setEnabled(flag);
	}

	public void thingsEnabled(boolean flag)
	{ 
		channelEnabled(flag);
		foldEnabled(flag);
		goEnabled(flag);

		feL.setEnabled(flag);
		fsL.setEnabled(flag);
		classL.setEnabled(flag);

		fexBox.setEnabled(flag);
		fslBox.setEnabled(flag);
		classifierBox.setEnabled(flag);
		
		outputPanel.clearEnabled(flag);
	}
}